package sad.controle;

import sad.util.Constantes;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import sad.dao.DAO;
import sad.dao.SubTopicoTableDAO;
import sad.util.DAOEvent;
import sad.visao.DialogAbstrato;
import sad.visao.FabricaDeDialog;
import sad.visao.ListSubTopDialog;

public class RelatorioSubTopControle extends AbstractControl{

  private ListSubTopDialog v_rel_s_top;

  public RelatorioSubTopControle(DialogAbstrato v) {
    this.v_rel_s_top = (ListSubTopDialog) v;
    addOuvinte();
    refreshRelatorio();
    v_rel_s_top.setModal(true);
    v_rel_s_top.setVisible(true);
  }
  
// <editor-fold defaultstate="collapsed" desc="Responde a eventos com origem nas visões">
    @Override
    public void actionPerformed(ActionEvent e) {
        Integer lineIndex = (Integer) v_rel_s_top.getSelectedSubTopico()[3];
        if(e.getActionCommand().equals(Constantes.NOVO_RELATORIO)){
            v_rel_s_top.resetMessage();
            FabricaDeDialog.criarDialog(null, "Sub Topico",null);
        }
        if(e.getActionCommand().equals(Constantes.EDITAR_SUBTOPICO)){
            v_rel_s_top.resetMessage();
            if(lineIndex==null){
                SubTopicoTableDAO.getInstance().erroField("vazio", "Sub Topico");
            }else{
                Object[] editable = v_rel_s_top.getSelectedSubTopico();
                SubTopicoTableDAO.getInstance().setIndexSubTop(lineIndex);
                FabricaDeDialog.criarDialog(null, "Sub Topico", editable);
            }
        }
        if(e.getActionCommand().equals(Constantes.CANCELAR_RELATORIO)){
            SubTopicoTableDAO.getInstance().removeDAOOuvinte(this);
            v_rel_s_top.close();
        }
        if(e.getActionCommand().equals(Constantes.EXCLUIR)){
            if(lineIndex==null){
                SubTopicoTableDAO.getInstance().erroField("vazio", "Sub Topico");
            }else{
                SubTopicoTableDAO.getInstance().removeSubTopico();
                //seleciona o professor anterior ao apagado
                lineIndex = (lineIndex < 0) ? 0 : lineIndex-1;
                if (lineIndex >= 0) {
                    v_rel_s_top.setSelectedRow(lineIndex);
                }
            }        
        }
    }

    @Override
    public void windowClosing(WindowEvent e){
        switch(e.getID()){
           case WindowEvent.WINDOW_CLOSING:
                SubTopicoTableDAO.getInstance().removeDAOOuvinte(this);
                DAO.closeObjectContainer();
                v_rel_s_top.close();
                break;
        }
    }// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Enchaminha eventos com origem no DAO">
    @Override
    public void daoChange(DAOEvent e) {
        v_rel_s_top.modelDAOChange(e);
    }// </editor-fold>
    
// <editor-fold defaultstate="collapsed" desc="Métodos privados comuns aos Relatórios">     
    private void addOuvinte() {
        v_rel_s_top.addActionListener(this);
        v_rel_s_top.addWindowOuvinte(this);
        SubTopicoTableDAO.getInstance().addDAOOuvinte(this);
    }

    private void refreshRelatorio() {
        SubTopicoTableDAO.create();
        v_rel_s_top.setTblSubTopicos(
                SubTopicoTableDAO.getInstance().getTableSubTopico());
    }// </editor-fold>
}
